#include"../httplib.h"
#include<iostream>
using namespace std;
void HelloWorld(const httplib::Request& req, httplib::Response& rsp){
  cout << req.method << endl;
  cout << req.path << endl;
  cout << "body:" << req.body << endl;
  rsp.body = "Hello World";
  rsp.status = 200;
}

void Numbers(const httplib::Request& req, httplib::Response& rsp){
  std::string num = req.matches[1];//mathches[0]是numbers
  cout << "Numbers" <<endl;
  //matches 测试
  //cout << typeid(req.matches).name() << endl;
  //cout << typeid(req.matches[0]).name() << endl;
  //matches类型 std::smatch(正则表达式)
  cout << req.matches[0] << endl;
  cout << "1:" << req.matches[1] << endl;
  cout << "2:" << req.matches[2] << endl;
  cout << "3:" << req.matches[3] << endl;

  rsp.body = num;
  rsp.status = 200;
}

void Dish(const httplib::Request& req, httplib::Response& rsp){
  rsp.body = req.body;
  rsp.status = 200;
}
int main(){
  cout << "主函数开始运行" << endl;
  httplib::Server srv;
  //当客户端请求的是一个静态的实体文件资源内的时候，就会直接读取文件数据进行响应
  srv.set_mount_point("/", "./wwwroot");//设置静态资源根目录  /index.html ->./wwwroot/index.html
  cout << "1" << endl;
  srv.Get("/hi", HelloWorld);//GET /hi HTTP/1.1
  //正则表达式：字符串的匹配资源   \d+ 表示匹配一个或多个连续的数字字符
  //srv.Get(R"(/numbers/(\d+))", Numbers);//GET /numbers/123 HTTP/1.1
  srv.Get(R"(/numbers/(\d+)/zl/(\d+))", Numbers);//matches测试
  //测试数据：numbers/123/zl/456 ---- 1:123   2:456   
  cout << "2" << endl;
  srv.Post("/dish", Dish);
  //搭建tcp服务器，开始监听，获取新连接，接收http请求数据，进行解析，调用对应处理函数 ，组织响应k
  cout << "3" << endl;
  srv.listen("0.0.0.0",9111);
  cout << "服务器开始监听" << endl;
  return 0;
}

